package com.nanhang.controller;

import com.nanhang.entity.TbSpecification;
import com.nanhang.service.SpecificationService;
import com.nanhang.util.PageResult;
import com.nanhang.util.R;
import com.nanhang.vo.SpecificationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping ("/specification")
public class SpecificationController {

    @Autowired
    private SpecificationService specificationService;

    // 获取规格列表
    @GetMapping ("/list")
    public List<TbSpecification> list () {
        return specificationService.findAll();
    }

    //2. 分页获取规格信息
    @GetMapping("/findByPage")
    public PageResult<TbSpecification> findByPage(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize){
        return specificationService.findByPage(page,pageSize);
    }

    // 3. 条件查询带分页
    // @RequestBody : 将前端传入的json对象转换为java对象
    // @ResponseBody : 将java对象转换为json对象
    @PostMapping("/search")
    public PageResult<TbSpecification> search(@RequestParam("page") Integer page,
                                      @RequestParam("pageSize") Integer pageSize,
                                      @RequestBody(required = false) SpecificationVO specificationVO){
        return specificationService.search(page,pageSize,specificationVO);
    }

    // 4. 新增规格
    @PostMapping("/save")
    public R save(@RequestBody TbSpecification tbSpecification){
        return specificationService.save(tbSpecification) ? R.ok().message("添加成功！") : R.error().message("添加失败！");
    }
}
